package cn.tedu.boot02.controller;

import cn.tedu.boot02.entity.Product;
import cn.tedu.boot02.utils.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@RestController
public class ProductController {
    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product = " + product);
        try (Connection conn = DBUtil.getConnection()){
            String sql = "INSERT INTO product VALUES(NULL,?,?,?)";
            //创建执行SQL语句的对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换掉里面的?
            ps.setString(1, product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            //执行SQL语句
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "添加完成!<a href='/'>返回首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        ArrayList<Product> list=new ArrayList<>();
        try (Connection conn=DBUtil.getConnection()){
            //准备查询的sql语句id，title，price,num
            String sql="SELECT * FROM product";
            //创建执行sql语句的对象
            PreparedStatement ps=conn.prepareStatement(sql);
            //执行查询sql，得到结果集对象
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                Product product=new Product();
                product.setId(rs.getInt(1));
                product.setTitle(rs.getString(2));
                product.setPrice(rs.getDouble(3));
                product.setNum(rs.getInt(4));
                //把对象装进集合
                list.add(product);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html="<table border=1>";
        html+="<caption>商品列表</caption>";
        html+="<tr><td>id</td><td>标题</td><td>价格</td><td>库存</td></tr>";
        for(Product product:list){
            html+="<tr>";
            html+="<td>"+product.getId()+"</td>";
            html+="<td>"+product.getTitle()+"</td>";
            html+="<td>"+product.getPrice()+"</td>";
            html+="<td>"+product.getNum()+"</td>";
            html+="<td><a href='/delete?id="+product.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = "+id);
        try ( Connection conn=DBUtil.getConnection()){
           String sql="DELETE FROM product WHERE id=?";
           PreparedStatement ps=conn.prepareStatement(sql);
           ps.setInt(1,id);
           ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除成功！<a href='/select'>返回列表首页</a>";
    }

    @RequestMapping("/update")
    public String update(Product product){
        try ( Connection conn=DBUtil.getConnection()){
           String sql="UPDATE product SET title=?,price=?,num=? WHERE id=?";
           PreparedStatement ps=conn.prepareStatement(sql);
           ps.setString(1, product.getTitle());
           ps.setDouble(2,product.getPrice());
           ps.setInt(3,product.getNum());
           ps.setInt(4,product.getId());
           ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'> 返回列表首页,</a>";
    }


}
